<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


class TravelController extends Controller
{
    
    public function getData(Request $request){
        $id = $request->input('id',0);
        $data = DB::table('travel_plan')->where('id',$id)->first();
        
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'计划不存在']);
        }
        
        $data =  objectToArray($data);
        
        if (!empty($data['price_json'])){
            $data['price_arr'] = json_decode($data['price_json'],true);
        }
        if (!empty($data['attraction_ison'])){
            $data['attraction_arr'] = json_decode($data['attraction_ison'],true);
        }
        
        return response()->json(['code'=>0,'data'=>$data]);
    }
    
    
    public function addPlanTwo(Request $request){

        $id = $request->input('id',0);
        $image = $request->input('image','');
        $plan_share_days = $request->input('plan_share_days',0);
        $price_json = $request->input('price_json','');
        $attraction_ison = $request->input('attraction_ison','');
        $start_city = $request->input('start_city','');
        $end_city = $request->input('end_city','');
        $start_time = $request->input('start_time','');
        $end_time = $request->input('end_time','');
        $all_price = $request->input('all_price','');

//         $price_arr = [
//             ['price_name'=>'xx','num'=>2,'standard'=>'标准','price'=>1000,'exchange_rate'=>'汇率'],
//             ['price_name'=>'xx','num'=>2,'standard'=>'标准','price'=>1000,'exchange_rate'=>'汇率'],
//             ['price_name'=>'xx','num'=>2,'standard'=>'标准','price'=>1000,'exchange_rate'=>'汇率']
//         ];
//         $price_json = '[{"price_name":"xx","num":2,"standard":"\u6807\u51c6","price":1000,"exchange_rate":"\u6c47\u7387"},{"price_name":"xx","num":2,"standard":"\u6807\u51c6","price":1000,"exchange_rate":"\u6c47\u7387"},{"price_name":"xx","num":2,"standard":"\u6807\u51c6","price":1000,"exchange_rate":"\u6c47\u7387"}]';
//         $attraction_arr = [
//             ['name'=>'xx','peer_num'=>2,'price'=>'167','num'=>1000,'exchange_rate'=>'汇率'],
//             ['name'=>'xx','peer_num'=>2,'price'=>'167','num'=>1000,'exchange_rate'=>'汇率'],
//             ['name'=>'xx','peer_num'=>2,'price'=>'167','num'=>1000,'exchange_rate'=>'汇率']
//         ];
//         $attraction_json = '[{"name":"xx","peer_num":2,"price":"167","num":1000,"exchange_rate":"\u6c47\u7387"},{"name":"xx","peer_num":2,"price":"167","num":1000,"exchange_rate":"\u6c47\u7387"},{"name":"xx","peer_num":2,"price":"167","num":1000,"exchange_rate":"\u6c47\u7387"}]';

        $data = DB::table('travel_plan')->where('id',$id)->first();
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'计划不存在']);
        }
        
        $temp = [
            'image'=>base64img($image),
            'plan_share_days'=>$plan_share_days,
            'price_json'=>$price_json,
            'attraction_ison'=>$attraction_ison,
            'start_city'=>$start_city,
            'end_city'=>$end_city,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'all_price'=>$all_price,
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        $res = DB::table('travel_plan')->where('id',$id)->update($temp);
        
        $temp['id'] = $id; 

        return response()->json(['code'=>0,'data'=>$temp]);

    }
    
    
    public function addPlanThree(Request $request){
        
        $id = $request->input('id',0);
        $title = $request->input('title','');
        $remark = $request->input('remark','');
        $is_lock = $request->input('is_lock','');
        $is_publish = $request->input('is_publish','');
        
        
        $data = DB::table('travel_plan')->where('id',$id)->first();
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'计划不存在']);
        }
        
        $temp = [
            'title'=>$title,
            'remark'=>$remark,
            'is_lock'=>$is_lock,
            'is_publish'=>$is_publish,
            'updated_at'=>date('Y-m-d H:i:s'),
            
        ];
        $res = DB::table('travel_plan')->where('id',$id)->update($temp);
        
        $temp['id'] = $id;
        
        return response()->json(['code'=>0,'data'=>$temp]);
          
    }
    
    public function statusLock(Request $request){
        $id = $request->input('id',0);
        $is_lock = $request->input('is_lock',1);
        $temp = [
            'is_lock'=>$is_lock,
            'updated_at'=>date('Y-m-d H:i:s'),
            
        ];
        $res = DB::table('travel_plan')->where('id',$id)->update($temp);
        
        $temp['id'] = $id;
        return response()->json(['code'=>0,'data'=>$temp]);
        
    }
    
    public function statusComplete(Request $request){
        $id = $request->input('id',0);
        $success_status = $request->input('success_status',2);
        $temp = [
            'success_status'=>$success_status,
            'updated_at'=>date('Y-m-d H:i:s'),
            
        ];
        $res = DB::table('travel_plan')->where('id',$id)->update($temp);
        
        $temp['id'] = $id;
        return response()->json(['code'=>0,'data'=>$temp]);
        
    }
    
    public function share(Request $request){
        $plan_id = $request->input('plan_id',0);
        $user_id = $request->input('user_id',0);
        if(empty($plan_id) || empty($user_id)){
            return response()->json(['code'=>10002,'msg'=>'参数不全']);
        }
        $data = DB::table('share')->where('plan_id',$plan_id)->where('user_id',$user_id)->first();
        if($data){
            return response()->json(['code'=>0,'msg'=>'再次分享，无须添加记录']);
        }
        $temp = [
            'plan_id'=>$plan_id,
            'user_id'=>$user_id,
            'created_at'=>date('Y-m-d H:i:s'),
            
        ];
        $res = DB::table('share')->insertGetId($temp);
        
        $temp['id'] = $res;
        return response()->json(['code'=>0,'data'=>$temp]);
        
    }
    
    public function getFind(Request $request){
        $city = DB::table('travel_plan')->where('is_publish',2)->groupBy('start_city')->get();
        $city =  objectToArray($city);
        $city_arr = [];
        if ($city){
            $city_arr = array_column($city, 'start_city');
        }
        $city_name = $request->input('city','all');
        if ($city_name=='all'){
            $lists = DB::table('travel_plan')->where('is_publish',2)->orderBy('id','desc')->paginate(8);
        }else{
            $lists = DB::table('travel_plan')->where('is_publish',2)->where('start_city',$city_name)->orderBy('id','desc')->paginate(8);
        }
        $lists =  objectToArray($lists);
        $data = [
            'city'=>$city_name,
            'city_arr'=>$city_arr,
            'lists'=>$lists,
        ];
        return response()->json(['code'=>0,'data'=>$data]);
    }
    
    public function allSave(Request $request){
        $id = $request->input('id',0);
        $image = $request->input('image','');
        $plan_share_days = $request->input('plan_share_days',0);
        $price_json = $request->input('price_json','');
        $attraction_ison = $request->input('attraction_ison','');
        $start_city = $request->input('start_city','');
        $end_city = $request->input('end_city','');
        $start_time = $request->input('start_time','');
        $end_time = $request->input('end_time','');
        $all_price = $request->input('all_price','');
        $title = $request->input('title','');
        $remark = $request->input('remark','');
        $is_lock = $request->input('is_lock','');
        $is_publish = $request->input('is_publish','');
        
        $data = DB::table('travel_plan')->where('id',$id)->first();
        if (empty($data)){
            return response()->json(['code'=>10002,'msg'=>'计划不存在']);
        }
        
        $temp = [
            'image'=>base64img($image),
            'plan_share_days'=>$plan_share_days,
            'price_json'=>$price_json,
            'attraction_ison'=>$attraction_ison,
            'start_city'=>$start_city,
            'end_city'=>$end_city,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'all_price'=>$all_price,
            'title'=>$title,
            'remark'=>$remark,
            'is_lock'=>$is_lock,
            'is_publish'=>$is_publish,
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        
        $res = DB::table('travel_plan')->where('id',$id)->update($temp);
        
        $temp['id'] = $id;
        
        return response()->json(['code'=>0,'data'=>$temp]);
        
    }
    
    
    
    
}